<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /*
    所有的前序和，判断对K取模是否为0，为0则结果+1
    */
    var subarraysDivByK = function (A, K) {
      let res = 0;
      for (let i = 0; i < A.length; i++) {
        let prev = 0;
        for (let j = i; j >= 0; j--) {
          prev += A[j];
          if (prev % K === 0) {
            res++
          }
        }
      }
      return res;
    };
    console.log(subarraysDivByK(nums = [4, 5, 0, -2, -3, 1], k = 5))
  </script>
</body>

</html>